<?		
	class View
	{	
		const Directory = 'lib/views/';
		
		protected $model;
		protected $controller;
		protected $name;
		protected $vars = array();
		protected $culture;
		
		private $sections = array();
		private $current = null;
		private $sectionBody = null;
		private $masterView = null;
		
		function __construct($model, $controller, $name)
		{			
			$this->model = $model;
			$this->controller = $controller;
			$this->name = $name;
			
			$this->culture = $controller->routeData['culture'];
		}
		
		public function master($name)
		{
			$this->masterView = new View($this->model, $this->controller, $name);
		}
		
		public function build($sections = array(), $vars = array(), $body = null)
		{
			foreach($sections as $key=>$val)
			{
				$this->sections[$key] = $val;
			}
			
			foreach($vars as $key=>$val)
			{
				$this->set($key, $val);
			}
			
			$this->sectionBody = $body;
			$file =  self::Directory . $this->name . '.php';			
			
			ob_start();

			if(is_file($file))
			{
				include_once($file);
			}

			$this->sectionBody = ob_get_clean();
			
			if(is_null($this->masterView))
			{
				$this->body();
			}
			else
			{
				$this->masterView->build($this->sections, $this->vars, $this->sectionBody);
			}
		}
		
		public function start($name)
		{
			ob_start();
			
			$this->current = $name;
		}
		
		public function end()
		{
			$content = ob_get_clean();
			
			if(empty($this->sections[$this->current]))
			{
				$this->sections[$this->current] = $content;
			}
		}
		
		public function section($name)
		{
			print $this->sections[$name];
		}
		
		public function body()
		{
            print $this->sectionBody;
		}
		
		public function set($key, $val)
		{
			$this->vars[$key] = $val;
		}
		
		public function get($key)
		{
			return $this->vars[$key];
		}
		
		public function action($controller, $name, $values = array())
		{
			return $this->url("$controller/$name/" . join('/', $values));
		}
		
		public function url($url)
		{
			return App::get('root') . $url;
		}

        public function cssInc($url, $root = true)
        {
			if($root) $url = $this->url($url);
			
            print '<link rel="stylesheet" type="text/css" href="' . $url . '">' . "\n";
        }

        public function jsInc($url, $root = true)
        {
			if($root) $url = $this->url($url);
			
            print '<script type="text/javascript" src="' . $url . '"></script>' . "\n";
        }

        public function text($key)
        {
			return $this->controller->words[$key];
        }
	}
?>